import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: bilesja
 * Date: 4/25/11
 * Time: 9:23 PM
 * To change this template use File | Settings | File Templates.
 */


public class GUI extends  JFrame {
    JFrame frame;
    Hash hash;
    int low_x;
    int low_y;
    int height;
    int width;
    int num_columns;
    int num_rows;
    JPanel myPanel;
    ArrayList<Coordinates> results;

    GUI(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getParams();
        hash = new Hash(height,width,num_columns,num_rows);

        ActionListener addFromFileListener = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
              FileCoordinateScanner.scanner(frame, hash);
            }
          };

        ActionListener addPointListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int x = askForX();
                int y = askForY();
                hash.addCoordinate(x,y);
            }
        };

        ActionListener findPointsListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int x = askForX();
                int y = askForY();
                results = hash.find_near(10, x, y);
                String returnString = "";
                for (int i = 0; i<results.size(); i++){
                    returnString = returnString.concat(results.get(i).printCoordinates()+"\n");
                }
                JOptionPane.showMessageDialog(frame, returnString);
            }
        };


        JToolBar toolbar = new JToolBar();
        toolbar.setRollover(true);
        JButton Jbutton1 = new JButton("Add from file");
        Jbutton1.addActionListener(addFromFileListener);
        toolbar.add(Jbutton1);
        toolbar.addSeparator();
        JButton Jbutton2 = new JButton("Add manually");
        Jbutton2.addActionListener(addPointListener);
        toolbar.add(Jbutton2);
        toolbar.addSeparator();
        JButton Jbutton3 = new JButton("Find 10 nearest");
        Jbutton3.addActionListener(findPointsListener);
        toolbar.add(Jbutton3);
        Container contentPane = frame.getContentPane();
        contentPane.add(toolbar, BorderLayout.NORTH);
        JTextArea textArea = new JTextArea();
        JScrollPane pane = new JScrollPane(textArea);
        contentPane.add(pane, BorderLayout.CENTER);
        frame.setSize(450, 65);
        frame.setVisible(true);
        setVisible(true);
    }

    private int askForX (){
        int x  = (int)(100*Double.parseDouble((String)JOptionPane.showInputDialog(frame,
                "What is the x coordinate of the point you are adding?", "custom dialog", JOptionPane.PLAIN_MESSAGE,null,null,null)));
        if (x < low_x || x > low_x + width) {
            JOptionPane.showMessageDialog(frame, "The x value must be between the lower and upper values you gave for x.");
            return askForX();
        } else return x;
    }

    private int askForY (){
        int y  =  (int)(100*Double.parseDouble((String)JOptionPane.showInputDialog(frame,
                "What is the y coordinate of the point you are adding?", "custom dialog", JOptionPane.PLAIN_MESSAGE,null,null,null)));
        if (y < low_y || y > low_y + width) {
            JOptionPane.showMessageDialog(frame, "The y value must be between the lower and upper values you gave for y.");
            return askForY();
        } else return y;
    }

    private void getParams(){
        //commented everything out for testing purposes
        boolean problem = true;
        while (problem){
            problem = false;
            low_x  = (int)(100*Double.parseDouble((String)JOptionPane.showInputDialog(frame,
                    "What is the smallest expected x coordinate?", "custom dialog", JOptionPane.PLAIN_MESSAGE,null,null,null)));
            int high_x  = (int)(100*Double.parseDouble((String)JOptionPane.showInputDialog(frame,
                    "What is the largest expected x coordinate?", "custom dialog", JOptionPane.PLAIN_MESSAGE,null,null,null)));
            width = high_x - low_x;
            if (width <= 0) {
                problem = true;
                JOptionPane.showMessageDialog(frame, "The upper x value needs to be larger than the lower x value");
            }
        }

        problem = true;
        while (problem){
            problem = false;
            low_y = (int)(100*Double.parseDouble((String)JOptionPane.showInputDialog(frame,
                    "What is the smallest expected y coordinate?", "custom dialog", JOptionPane.PLAIN_MESSAGE,null,null,null)));
            int high_y =(int)(100*Double.parseDouble((String)JOptionPane.showInputDialog(frame,
                    "What is the largest expected y coordinate?", "custom dialog", JOptionPane.PLAIN_MESSAGE,null,null,null)));
            height = high_y - low_y;
            if (height <= 0) {
                problem = true;
                JOptionPane.showMessageDialog(this, "The upper y value needs to be larger than the lower y value");
            }
        }
        int num_items = Integer.parseInt((String)JOptionPane.showInputDialog(frame,
                    "How many items are you expecting to add? (Putting in a wildly inaccurate number can effect runtime)", "custom dialog", JOptionPane.PLAIN_MESSAGE,null,null,null));


     /*   low_x = 0;
        low_y = 0;
        width = 1000;
        height = 1000;
        int num_items = 10;       */



        //these calculations will make it so there is approximately 1 item per bigArea.
        int area = height*width;
        int density = num_items/area;
        num_rows = (int)(Math.sqrt(num_items)*width/height)+1;
        num_columns = (int)(Math.sqrt(num_items)*height/width)+1;
    }


    public static void main(String[] args){
        new GUI();
    }


    public void itemStateChanged(ItemEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
